<template>
    <div >
    <div style="margin-bottom: 10px">
        <el-input size="small" v-model="addColumn" placeholder="新增栏目名称" style="width: 200px;margin-right: 8px"></el-input>
        <el-button size="small" type="primary" @click="add">新增栏目</el-button>
    </div>
    <div style="height: 450px">
        <el-table v-loading="loading"
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%;background-color: mistyrose"
                @selection-change="handleSelectionChange"
              >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="id"
                    label="编号"
                    width="120">
            </el-table-column>
            <el-table-column
                    prop="catename"
                    label="栏目名称"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="enabletime"
                    label="启用时间"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini"  @click="showEdit(scope.row)" type="butten" >编辑</el-button>
                    <el-button size="mini" type="button" style="background: orangered; color: white"  @click="open(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <hr/>
        <!--total是 后端查出来的所有数据
        page-count是组件自家带的当前页属性， pageNum是绑定的一个当前页，要和后端参数对应
        page-size是组件自家带的一页显示多少条数据，默认10条，pageSize是绑定的一页显示多少条数据，要和后端参数对应
        current-change是组件自家带的改变当前页，绑定一个函数
        -->
        <!--批量删除按钮-->

            <el-button type="danger" size="small" :disabled="multiDleBtn" @click="multiDel">批量删除</el-button>

        <!-- 页码   -->
        <el-pagination
                background
                layout="prev, pager, next,sizes,total"
                :page-count="pageNum"
                :page-size="pageSize"
                :page-sizes="[3,4,5]"
                @size-change="handleSizeChange"
                :total="total"
                @current-change="changePage"
                style="float: right;margin-right: 100px" >
        </el-pagination>
    </div>
<!--  编辑弹出框-->
            <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
                <div>
                    <span >请输入新栏目名称</span>
                    <el-input class="q" size="small" v-model="updeteColumn.catename" placeholder="栏目名称" style="width:100%;"></el-input>
                </div>
                <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="updeteC">修 改</el-button>
                </span>
            </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Column",
        data(){
            return {
                addColumn:'',
                tableData:[],
                dialogVisible: false,//控制是否弹窗
                pageNum:1,
                pageSize:3,
                total:0,
                loading: true,//加载
                updeteColumn:{
                    catename:'',
                    id:''
                },
                multiDelArr: [],//批量删除数组
                multiDleBtn:true//true的时候按钮禁用
            }
        },methods:{
            //批量删除
            multiDel(){
                this.$confirm('此操作将永久删除'+this.multiDelArr.length+'条数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let str='';
                    this.multiDelArr.forEach((item,i)=>{
                        str += 'ids='+item.id+'&';
                    })
                    this.deleteRequest('/cat/multiDel?'+str).then(resp=>{
                       if(resp){
                           this.getALLColumn();//重新查询
                       }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {//监控按钮禁用或不禁用
                if(val.length>0){
                    this.multiDleBtn=false;
                    this.multiDelArr=val;//val就是选中的数据集合
                }else {
                    this.multiDleBtn=true;
                }
            },
            showEdit(data){//scope.row是表格当前一行数据
              this.dialogVisible=true;
              this.updeteColumn.id=data.id;
              this.updeteColumn.catename=data.catename;
            },
            updeteC(){
                this.putRequest('/cat/updateCategory',this.updeteColumn).then(resp=>{
                    if(resp){
                        this.dialogVisible=false;
                        this.getALLColumn();
                    }else {
                        this.$message.error("错误");
                    }
                })
            },
            handleSizeChange(val) {
                this.pageSize=val;
                this.getALLColumn();//重新查询
            },
            changePage(data){//函数自带data表示就是第几页
                this.pageNum=data;
                this.getALLColumn();//重新查询
            },
            open(data) {//删除弹出框
                this.$confirm('此操作将永久删除['+data.catename+'], 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/cat/delCategory/'+data.id).then(resp=>{
                        this.getALLColumn();//重新查询
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            add(){
                if(this.addColumn==''||this.addColumn.trim()==''){
                    this.$message.warning("不能为空");
                }else{
                    let  c={
                        catename:this.addColumn
                    }
                    this.postRequest('/cat/addCategory/',c).then(resp=>{
                        this.getALLColumn()
                    })
                }

            },
            //查询
            getALLColumn(){
                this.getRequest('/cat/getCategory?pageNum='+this.pageNum+'&pageSize='+this.pageSize).then(resp=>{
                    this.tableData=resp.list;//现在的resp是pageInfo，自带的list才是tableData要的集合
                    this.total=resp.total;//同上
                    this.loading=false;
                })
            }
        },
        mounted() {
            this.getALLColumn();
        }

    }
</script>

<style scoped>

.q{
    margin-top: 20px;
}
</style>